package thrid.internet;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.test.R;

import java.security.MessageDigest;
import java.util.SimpleTimeZone;

import thrid.internet.transfromation.CircularTransformation;
import thrid.internet.transfromation.RoundTransformation;

import static com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions.withCrossFade;

/**
 * Created by RuiJiao Fang on 2018-05-22.
 */
public class LoadImage {

    private static volatile LoadImage loadImage;

    private LoadImage(){

    }

    public static LoadImage in(){
        if(loadImage == null){
            synchronized (LoadImage.class){
                if(loadImage == null){
                    loadImage = new LoadImage();
                }
            }
        }
        return loadImage;
    }

    @SuppressLint("Range")
    public void loadImage(Context context, String url, ImageView imageView){
        onGlideTransfrom(context,url,imageView);
    }

    private void onGlideTarget(Context context, String url, final ImageView imageView){
        SimpleTarget<Bitmap> simpleTarget = new SimpleTarget<Bitmap>(100,100) {
            @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                imageView.setImageBitmap(resource);
            }
        };
        GlideApp.with(context)
                .asBitmap()
                .load(url)
                .into(simpleTarget);
    }

    /**
     * 图片处理
     * @param context
     * @param url
     * @param imageView
     */
    private void onGlideTransfrom(Context context,String url, final ImageView imageView){
        //圆形裁剪
        GlideApp.with(context)
                .asBitmap()
                .load(url)
                .transform(new CircularTransformation(context))
                .into(imageView);
    }

    private void onGlideSimple(Context context, String url, ImageView imageView){
        GlideApp.with(context)
                .load(url)
                .override(300)
                .placeholder(R.mipmap.ic_launcher)
                .error(R.mipmap.ic_launcher_round)
                .fallback(R.mipmap.ic_launcher_round) //请求url或者图片为null，则使用此磨人图片
                .transition(withCrossFade())
                .priority(Priority.HIGH) //设置本次图片加载优先级
                .thumbnail(0.1f)  //缩略图
                .into(imageView);

        GlideApp.with(context)
                .load(url)
                .into(imageView);
    }



    public void clerImage(Context context,ImageView imageView){
        Glide.with(context)
                .clear(imageView);
    }
}
